package cn.com.freelibra.forum.web;

import java.util.Date;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.Page;

import cn.com.freelibra.forum.po.Section;
import cn.com.freelibra.forum.po.User;
import cn.com.freelibra.forum.service.SectionDetailService;
import cn.com.freelibra.forum.service.SectionService;
import cn.com.freelibra.forum.util.HtmlUtil;
import cn.com.freelibra.forum.web.view.AjaxObject;

/**
 * 帖子
 * @author qing
 *
 */
@Controller
@RequestMapping("/section")
public class SectionController {
	
	@Resource
	private SectionService sectionService;
	@Resource
	private SectionDetailService sectionDetailService;
	
	/**
	 * 帖子
	 * @param model
	 * @param sectionId
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@RequestMapping("/sectionDetail")
	public String sectionDetail(Model model,Long sectionId,Integer pageNum,Integer pageSize){
		Map<String, Object> map = sectionService.selectSectionJoinUserByOne(sectionId);
		Page<Map<String, Object>> mapList = sectionDetailService.selectSectionDetailJoinUserList(sectionId, pageNum, pageSize);
		model.addAttribute("section", map);
		model.addAttribute("sectionDetailList", mapList);
		model.addAttribute("pageNum", mapList.getPageNum());
		model.addAttribute("totalPages", mapList.getPages());
		return "/section-detail/add";
	}
	
	/**
	 * 我的帖子
	 * @param model
	 * @param sectionId
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@RequestMapping("/my-section")
	public String mySection(Model model,String keyword,Integer pageNum,Integer pageSize){
		Page<Map<String, Object>> result = sectionService.selectSectionJoinUserList(keyword, pageNum, pageSize);
		model.addAttribute("result", result);
		model.addAttribute("pageNum", result.getPageNum());
		model.addAttribute("totalPages", result.getPages());
		model.addAttribute("top_key_word",keyword);
		return "/section-detail/add";
	}
	
	/**
	 * 跳转发帖子
	 * @param model
	 * @param sectionId
	 * @param code
	 * @param context
	 * @param sectionImg
	 * @param name
	 * @return
	 */
	@RequestMapping("/add")
	public String sectionAdd(Model mode){
		return "/section/add";
	}
	
	/**
	 * 保存
	 * @param model
	 * @param sectionId
	 * @param code
	 * @param context
	 * @param sectionImg
	 * @param name
	 * @return
	 */
	@RequestMapping("/save")
	@ResponseBody
	public AjaxObject sectionSave(Model model,String context,String sectionImg,String name,HttpServletRequest request){
		AjaxObject ao = new AjaxObject();
		if(name==null||"".equals(name.trim())){
			ao.setStatus(401);//标题
			ao.setMsg("请输入标题！");//提示内容
			ao.setAction("alert");//弹窗提示
			return ao;
		}
		if(context==null||"".equals(context.trim())){
			ao.setStatus(402);//标题
			ao.setMsg("请输入内容！");//提示内容
			ao.setAction("alert");//弹窗提示
			return ao;
		}
		if(context.trim().length()<10){
			ao.setStatus(403);//标题
			ao.setMsg("内容不能少于10个字");//提示内容
			ao.setAction("alert");//弹窗提示
			return ao;
		}
		if(name.trim().length()>15){
			ao.setStatus(404);//标题
			ao.setMsg("标题不能超过15字");//提示内容
			ao.setAction("alert");//弹窗提示
			return ao;
		}
		User user = (User) request.getSession().getAttribute("user");
		if(user==null){
			ao.setStatus(405);//标题
			ao.setMsg("登陆状态已过期，请重新登陆");//提示内容
			ao.setAction("alert");//弹窗提示
			return ao;
		}else{
			Section section = new Section();
			section.setContext(context);
			section.setName(name);
			section.setUserId(user.getId());
			section.setCreateTime(new Date());
			section.setItStatus(0);
			String text = HtmlUtil.removeHtmlInTag(context);
			if(text.length()>100)
				section.setRemark(text.substring(0,100)+"...");
			else
				section.setRemark(text.substring(0,text.length())+"...");
			sectionService.add(section);
			ao.setStatus(200);
			ao.setMsg("发表帖子成功");//提示内容
			ao.setAction("alert");//弹窗提示
			return ao;
		}
	}
}
